<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="票据号" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入票据号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="状态" prop="status">-->
<!--        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.purchase_status"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['purchase:ryQuality:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ryDepotHeadList">
      <el-table-column label="票据号" align="center" prop="number"/>
      <el-table-column label="采购单号" align="center" prop="linkNumber"/>
      <el-table-column label="合同编号" align="center" prop="contractNumber"/>
      <el-table-column label="项目代号" align="center" prop="projectCode" />
      <el-table-column label="操作员" align="center" prop="operName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="状态" align="center" prop="status">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.purchase_status" :value="scope.row.status"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="项目状态" align="center" prop="projectStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.project_status" :value="scope.row.projectStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['purchase:ryQuality:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetails(scope.row)"
            v-hasPermi="['purchase:ryQuality:detail']"
          >详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改单据主表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row :gutter="10" class="mb8">
          <el-col :span="6">
            <el-form-item label="票据号">
              <el-input v-model="form.number" placeholder="请输入票据号" :readonly="true"/>
            </el-form-item>
          </el-col>

          <el-col :span="6" v-if="form.id == null ? true : false">
            <el-form-item label="采购订单" prop="linkHeadId">
              <el-select v-model="form.linkHeadId" clearable placeholder="请选择" @change="aa" filterable clearable>
                <el-option
                  v-for="item in linkHeadOptions"
                  :key="item.id"
                  :label="item.contractNumber + '('+ item.number  +')'"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="6" v-if="form.id != null ? true : false">
            <el-form-item label="采购订单" prop="linkNumber">
              <el-input v-model="form.contractNumber + '(' + form.linkNumber + ')'" placeholder="" :readonly="true"/>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row :gutter="10" class="mb8">
          <el-col :span="6" v-if="title !=='新增质检单'">
            <el-form-item label="创建时间:">
              <span>{{ parseTime(form.createTime) }}</span>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row :gutter="10" class="mb8">
          <div style="width: 100%;max-height:450px;overflow: auto;text-align: center">
            <el-table :data="tableData" border max-height="450">
              <el-table-column label="物品名称"  width="150" fixed>
                <template slot-scope="scope">
<!--                  <el-input placeholder="请输入物品名称" v-model="scope.row.inventoryName"></el-input>-->
                                    <span>{{scope.row.inventoryName}}</span>
                </template>
              </el-table-column>
              <el-table-column label="物品规格" fixed>
                <template slot-scope="scope">
                  <!--                <el-input placeholder="请输入物品规格" v-model="scope.row.inventorySpecifications"></el-input>-->
                  <span>{{ scope.row.inventorySpecifications }}</span>
                </template>
              </el-table-column>
              <el-table-column label="品牌" fixed>
                <template slot-scope="scope">
<!--                  <el-input placeholder="请输入供应商" v-model="scope.row.supply"></el-input>-->
                                  <span>{{scope.row.brand}}</span>
                </template>
              </el-table-column>
<!--              <el-table-column label="wbs编码">-->
<!--                <template slot-scope="scope">-->
<!--                  <el-input placeholder="请输入wbs编码" v-model="scope.row.wbs"></el-input>-->
<!--                  &lt;!&ndash;                <span>{{scope.row.wbs}}</span>&ndash;&gt;-->
<!--                </template>-->
<!--              </el-table-column>-->
<!--              <el-table-column label="备注">-->
<!--                <template slot-scope="scope">-->
<!--                  <el-input placeholder="请输入备注" v-model="scope.row.remark"></el-input>-->
<!--                  &lt;!&ndash;                <span>{{scope.row.remark}}</span>&ndash;&gt;-->
<!--                </template>-->
<!--              </el-table-column>-->
              <el-table-column label="待采购数量">
                <template slot-scope="scope">
<!--                  <el-input placeholder="请输入待采购数量" v-model="scope.row.advancePurchaseNum"></el-input>-->
                                  <span>{{scope.row.advancePurchaseNum}}</span>
                </template>
              </el-table-column>
              <el-table-column label="总库可用数量">
                <template slot-scope="scope">
                  <!--                  <el-input placeholder="请输入备注" v-model="scope.row.advancePurchaseNum"></el-input>-->
                  <span>{{scope.row.usableNum}}</span>
                </template>
              </el-table-column>
              <el-table-column label="实际采购数量">
                <template slot-scope="scope">
<!--                  <el-input placeholder="请输入采购数量" v-model="scope.row.actualPurchaseNum"></el-input>-->
                                  <span>{{scope.row.actualPurchaseNum}}</span>
                </template>
              </el-table-column>
<!--              <el-table-column label="发货状态">-->
<!--                <template slot-scope="scope">-->
<!--                    <span v-for="(item) in dict.type.shipping_status" v-if="scope.row.shippingStatus === item.value">-->
<!--                    {{item.label}}-->
<!--                  </span>-->
<!--                </template>-->
<!--              </el-table-column>-->
<!--              <el-table-column label="采购员">-->
<!--                <template slot-scope="scope">-->
<!--                  &lt;!&ndash;                <el-input placeholder="请输入姓名" v-model="scope.row.name"></el-input>&ndash;&gt;-->
<!--                  <span>{{ scope.row.purchaseMan }}</span>-->
<!--                </template>-->
<!--              </el-table-column>-->
              <el-table-column label="合格数量">
                <template slot-scope="scope">
<!--                  <el-input placeholder="请输入合格数量" v-model="scope.row.qualifiedNum"></el-input>-->
                  <!--                  <span>{{ scope.row.qualifiedNum }}</span>-->
                  <el-input-number placeholder="请输入合格数量" v-model="scope.row.qualifiedNum" style="width: 50px;text-align: left;padding: 0px 0px"
                                   size="small" :controls="false"
                                   :max="scope.row.actualPurchaseNum-scope.row.unqualifiedNum" :min="0"></el-input-number>
                </template>
              </el-table-column>
              <el-table-column label="不合格数量">
                <template slot-scope="scope">
<!--                  <el-input placeholder="请输入不合格数量" v-model="scope.row.unqualifiedNum"></el-input>-->
                  <!--                  <span>{{ scope.row.unqualifiedNum }}</span>-->
                  <el-input-number placeholder="请输入不合格数量" v-model="scope.row.unqualifiedNum" style="width: 50px;text-align: left;padding: 0px 0px"
                                   size="small" :controls="false"
                                   :max="scope.row.actualPurchaseNum-scope.row.qualifiedNum" :min="0"></el-input-number>
                </template>
              </el-table-column>
              <el-table-column label="不合格原因">
                <template slot-scope="scope">
                  <el-input placeholder="请输入不合格原因" v-model="scope.row.unqualifiedReason"></el-input>
                  <!--                  <span>{{ scope.row.unqualifiedReason }}</span>-->
                </template>
              </el-table-column>
              <el-table-column label="退回状态">
                <template slot-scope="scope">
                  <el-select v-model="scope.row.returnStatus" placeholder="请选择发货状态">
                    <el-option
                      v-for="dict in dict.type.return_status"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                  <!--                <span>{{scope.row.name}}</span>-->
                </template>
              </el-table-column>
              <el-table-column label="重发状态">
                <template slot-scope="scope">
                  <el-select v-model="scope.row.retransmissionStatus" placeholder="请选择发货状态">
                    <el-option
                      v-for="dict in dict.type.retransmission_status"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                  <!--                <span>{{scope.row.name}}</span>-->
                </template>
              </el-table-column>
              <el-table-column label="重发数量">
                <template slot-scope="scope">
                  <el-input placeholder="请输入重发数量" v-model="scope.row.retransmissionNum"></el-input>
                  <!--                  <span>{{ scope.row.retransmissionNum }}</span>-->
                </template>

              </el-table-column>
<!--              <el-table-column label="质检员">-->
<!--                <template slot-scope="scope">-->
<!--                  <el-input placeholder="请输入质检员" v-model="scope.row.qualityInspector"></el-input>-->
<!--                  &lt;!&ndash;                  <span>{{ scope.row.qualityInspector }}</span>&ndash;&gt;-->
<!--                </template>-->

<!--              </el-table-column>-->
<!--              <el-table-column label="质检时间" width="150">-->
<!--                <template slot-scope="scope" >-->
<!--                                <el-date-picker clearable-->
<!--                                                v-model="scope.row.qualityTime"-->

<!--                                                type="date"-->
<!--                                                value-format="yyyy-MM-dd"-->
<!--                                                placeholder="请选择质检时间"-->
<!--                                >-->
<!--                                </el-date-picker>-->
<!--&lt;!&ndash;                  <span>{{ scope.row.qualityTime }}</span>&ndash;&gt;-->
<!--                </template>-->

<!--              </el-table-column>-->
            </el-table>
          </div>
        </el-row>
        <el-form-item label="备注">
          <el-input v-model="form.remark"></el-input>
        </el-form-item>
        <el-form-item label="附件名称">
          <file-upload v-model="form.fileName"/>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!--    详情弹窗-->
    <el-dialog :title="title" :visible.sync="details" width="1200px">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="10" class="mb8">
          <el-col :span="6">
            <el-form-item label="票据号: ">
              <!--              <el-input v-model="form.number" placeholder="请输入票据号"/>-->
              <span>{{form.number}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="采购订单: " prop="LinkNumber">
              <!--              <el-input v-model="form.number" placeholder="请输入票据号"/>-->
              <span>{{form.contractNumber + '(' + form.linkNumber + ')'}}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10" class="mb8">
          <el-col :span="6">
            <el-form-item label="创建时间:">
              <span>{{ parseTime(form.createTime) }}</span>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row :gutter="10" class="mb8" v-show="false">
          <div style="width: 100%;overflow-x: auto;text-align: center">
            <el-table :data="tableData" border ref="report-table">
              <el-table-column label="票据号">
                <el-table-column prop="inventoryName" label="物品名称" width="150"></el-table-column>
                <el-table-column prop="inventorySpecifications" label="物品规格"></el-table-column>
                <el-table-column prop="brand" label="品牌"></el-table-column>
                <el-table-column prop="advancePurchaseNum" label="待采购数量"></el-table-column>
              </el-table-column>

              <el-table-column :label="form.number">
                <el-table-column prop="usableNum" label="总库可用数量"></el-table-column>
                <el-table-column prop="actualPurchaseNum" label="实际采购数量"></el-table-column>
<!--                <el-table-column label="发货状态">-->
<!--                  <template slot-scope="scope">-->
<!--                    <span v-for="(item) in dict.type.shipping_status" v-if="scope.row.shippingStatus === item.value">-->
<!--                    {{item.label}}-->
<!--                  </span>-->
<!--                  </template>-->
<!--                </el-table-column>-->
                <el-table-column prop="qualifiedNum" label="合格数量"></el-table-column>
              </el-table-column>


              <el-table-column label="采购订单">
                <el-table-column prop="unqualifiedNum" label="不合格数量"></el-table-column>
                <el-table-column prop="unqualifiedReason" label="不合格原因"></el-table-column>
                <el-table-column label="退回状态">
                  <template slot-scope="scope">
                    <span v-for="(item) in dict.type.return_status" v-if="scope.row.returnStatus === item.value">
                    {{item.label}}
                  </span>
                  </template>
                  <el-table-column label="重发状态">
                    <template slot-scope="scope">
                  <span v-for="(item,index) in dict.type.retransmission_status" v-if="scope.row.retransmissionStatus === item.value">
                    {{item.label}}
                  </span>
                    </template>
                  </el-table-column>
                </el-table-column>
              </el-table-column>

              <el-table-column :label="form.contractNumber + '(' + form.linkNumber + ')'">
                <el-table-column prop="retransmissionNum" label="重发数量"></el-table-column>
                <el-table-column prop="qualityInspector" label="质检员"></el-table-column>
                <el-table-column prop="qualityTime" label="质检时间"></el-table-column>
              </el-table-column>

            </el-table>
          </div>
        </el-row>
        <el-row :gutter="10" class="mb8" >
          <div style="width: 100%;overflow-x: auto;text-align: center">
            <el-table :data="tableData" border max-height="450">
              <el-table-column prop="inventoryName" label="物品名称" width="150"></el-table-column>
              <el-table-column prop="inventorySpecifications" label="物品规格" width="150"></el-table-column>
              <el-table-column prop="brand" label="品牌"></el-table-column>
              <el-table-column prop="advancePurchaseNum" label="待采购数量"></el-table-column>
              <el-table-column prop="usableNum" label="总库可用数量"></el-table-column>
              <el-table-column prop="actualPurchaseNum" label="实际采购数量"></el-table-column>
<!--              <el-table-column label="发货状态">-->
<!--                <template slot-scope="scope">-->
<!--                    <span v-for="(item) in dict.type.shipping_status" v-if="scope.row.shippingStatus === item.value">-->
<!--                    {{item.label}}-->
<!--                  </span>-->
<!--                </template>-->
<!--              </el-table-column>-->
              <el-table-column prop="qualifiedNum" label="合格数量"></el-table-column>
              <el-table-column prop="unqualifiedNum" label="不合格数量"></el-table-column>
              <el-table-column prop="unqualifiedReason" label="不合格原因"></el-table-column>
              <el-table-column label="退回状态">
                <template slot-scope="scope">
                    <span v-for="(item) in dict.type.return_status" v-if="scope.row.returnStatus === item.value">
                    {{item.label}}
                  </span>
                </template>
              </el-table-column>
              <el-table-column label="重发状态">
                <template slot-scope="scope">
                  <span v-for="(item,index) in dict.type.retransmission_status" v-if="scope.row.retransmissionStatus === item.value">
                    {{item.label}}
                  </span>
                  <!--                <span>{{scope.row.name}}</span>-->
                </template>
              </el-table-column>
              <el-table-column prop="retransmissionNum" label="重发数量"></el-table-column>
              <el-table-column prop="qualityInspector" label="质检员"></el-table-column>
              <el-table-column prop="qualityTime" label="质检时间"></el-table-column>
            </el-table>
          </div>
        </el-row>
      </el-form>
      <div slot="footer" style="text-align: center">
        <el-button type="success" size="medium" @click="exportExcel">导出</el-button>
      </div>

    </el-dialog>
  </div>
</template>

<script>
import {
  listRyDepotHead,
  dataprocess,
  getRyDepotHead,
  delRyDepotHead,
  addRyDepotHead,
  updateRyDepotHead
} from '@/api/purchase/ryDepotHead'
import * as XLSX from 'xlsx/xlsx.mjs'
import * as FileSaver from 'file-saver'
import axios from 'axios'
import { $message } from 'element-ui'
import { getToken } from '@/utils/auth'
import { blobValidate } from '@/utils/ruoyi'
import { listRyQuality } from '@/api/purchase/ryQuality'
import {formatDatee} from "@/utils";
import request from "@/utils/request";

export default {
  name: 'RyDepotHead',
  dicts: ['purchase_status', 'project_status', 'return_status', 'shipping_status', 'retransmission_status'],
  data() {
    return {
      ip: 'http://localhost:8080',
      count: 0,
      uploadData: [], //文件数据
      fileData: '', // 保存选择的文件
      list: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 单据主表表格数据
      ryDepotHeadList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectName: null,
        projectCode: null,
        contractNumber: null,
        number: null,
        status: null,
        projectStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        number: [
          { required: true, message: '请输入质检单编号', trigger: 'blur' }
        ],
        linkHeadId: [
          { required: true, message: '请关联采购单', trigger: 'blur' }
        ],
      },
      // 关联项目下拉框
      linkHeadOptions: [],
      tableData: [],
      details: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询单据主表列表 */
    getList() {
      this.loading = true
      listRyQuality(this.queryParams).then(response => {
        this.ryDepotHeadList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        projectName: null,
        projectCode: null,
        contractNumber: null,
        type: null,
        subType: null,
        defaultNumber: null,
        number: null,
        createTime: null,
        operTime: null,
        organId: null,
        billType: null,
        remark: null,
        fileName: null,
        salesMan: null,
        status: null,
        projectStatus: null,
        linkNumber: null,
        tenantId: null,
        delFlag: null,
        tableData: []
      }
      this.tableData = []
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    // handleSelectionChange(selection) {
    //   this.ids = selection.map(item => item.id)
    //   this.single = selection.length !== 1
    //   this.multiple = !selection.length
    // },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '新增质检单'
      this.form.number = 'CGZJ'+formatDatee()
      this.form.createTime = new Date()
      let that = this;
      return request({
        url: '/purchase/ryDepotHead/allForQualityOrder',
        method: 'get'
      }).then(function(res) {
        that.linkHeadOptions = res.data
        console.log(res);
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      let that =this
      console.log(row.projectStatus)
      if (row.projectStatus === '1'){
        that.$message({
          showClose: true,
          message: '项目已结束',
          type: 'warning'
        })
      }else {
        this.reset()
        const id = row.id || this.ids
        console.log(id)
        return request({
          url: '/purchase/ryQuality/selectRyDepotDetailById/' + id,
          method: 'get'
        }).then(function(res) {
          console.log(res)
          that.open = true
          that.title = '修改质检单'
          that.form = res.data
          that.tableData = res.data.tableData
          // for (let i = 0; i < res.data.tableData.length; i++) {
          //   res.data.tableData[i].qualifiedNum = res.data.tableData[i].actualPurchaseNum - res.data.tableData[i].unqualifiedNum
          //   that.tableData.push(res.data.tableData[i])
          // }
        })
        // getRyDepotHead(id).then(response => {
        //   this.form = response.data
        //   this.open = true
        //   this.title = '修改单据主表'
        // })
      }
    },
    /** 提交按钮 */
    submitForm() {
      let that = this
      this.form.tableData = this.tableData
      console.log(this.form)
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            return request({
              url: '/purchase/ryQuality/editQualityDetail',
              method: 'post',
              data: this.form
            }).then(function(res) {
              console.log(res)
              if (res.code === 200) {
                that.$message({
                  showClose: true,
                  message: '修改成功',
                  type: 'success'
                })
                that.open = false
                that.getList()
              } else {
                console.log('报错了')
                that.$message({
                  showClose: true,
                  message: res.msg,
                  duration: 0,
                  type: 'warning'
                })
              }
            }).catch(res => {
              console.log(res)
            })


          } else {
            return request({
              url: '/purchase/ryQuality/addDepotHeadAndDetail',
              method: 'post',
              data: this.form
            }).then(function(res) {
              console.log(res)
              that.$message({
                showClose: true,
                message: '新增成功',
                type: 'success'
              })
              that.open = false
              that.getList()
            }).catch(res => {
              console.log(res)
            })
          }
        }
      })
    },
    //详情
    handleDetails(row){
      let that = this
      return request({
        url: '/purchase/ryQuality/selectRyDepotDetailById/' + row.id,
        method: 'get'
      }).then(function(res) {
        console.log(res)
        if (res.code === 200) {
          that.details = true
          that.title = '采购质检详情';
          that.form = res.data;
          that.tableData = res.data.tableData;
        } else {
          console.log('报错了')
          that.$message({
            showClose: true,
            message: res.msg,
            duration: 0,
            type: 'warning'
          })
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除单据主表编号为"' + ids + '"的数据项？').then(function() {
        return delRyDepotHead(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('purchase/ryDepotHead/export', {
        ...this.queryParams
      }, `ryDepotHead_${new Date().getTime()}.xlsx`)
    },

    readExcel(files) {
      this.count += 1
      if (this.count % 3 === 1) {
        console.log('zhiixng')
        this.fileData = files // 保存当前选择文件
        let that = this
        // console.log(files.file.originFileObj)
        let type = files.file.name.split('.')
        if (!files) {
          //如果没有文件
          return false
        } else if (type[type.length - 1] !== 'xlsx' && type[type.length - 1] !== 'xls' && type[type.length - 1] !== 'et') {
          this.$message.error('上传格式不正确，请上传xls或者xlsx格式')
          return false
        }
        const fileReader = new FileReader()
        // 如果为原生 input 则应是 files[0]
        fileReader.readAsBinaryString(files.file.originFileObj)
        fileReader.onload = ev => {// 异步执行
          try {
            const data = ev.target.result
            const workbook = XLSX.read(data, {
              type: 'binary'
            })
            const wsname = workbook.SheetNames[0] //取第一张表
            const ws = XLSX.utils.sheet_to_json(workbook.Sheets[wsname]) //生成json表格内容
            that.uploadData = [] //清空接收数据

            for (let i = 6; i < ws.length; i++) {
              let arr = {}
              //   arr.wbs = ''
              for (const key in ws[i]) {
                switch (key) {
                  case 'LIST  明 细 表':
                    arr.name = ws[i][key].toUpperCase().replace(/\s*/g, '')
                    break
                  case '__EMPTY_1':
                    arr.drawing = ws[i][key]
                    break
                  case '__EMPTY_2':
                    arr.spec = ws[i][key].toUpperCase().replace(/\s*/g, '')
                    break
                  case '__EMPTY_3':
                    arr.supplier = ws[i][key]
                    break
                  case '__EMPTY_4':
                    arr.number = ws[i][key]
                    break
                  case '__EMPTY_6':
                    arr.wbs = ws[i][key]
                    break
                }
              }
              if (arr.name !== undefined) {
                this.list.push(arr)
                // if (this.list.length === 0) {
                //   console.log('dfgfg' )
                //   this.list.push(arr)
                // } else {
                //   console.log('-->'+this.list.length);
                //   let list11 = this.list;
                //   for (let j = 0; j < this.list.length; j++) {
                //     console.log("jjj-->"+j)
                //     console.log(this.list[j].spec)
                //     console.log(arr.spec)
                //     console.log(this.list[j].name)
                //     console.log(arr.name)
                //     if (this.list[j].spec === arr.spec && this.list[j].name === arr.name) {
                //
                //         if (this.list[j].spec === arr.spec && this.list[j].name === arr.name) {
                //           console.log('重复了')
                //           this.list[j].number += arr.number;
                //           length = null;
                //           break;
                //       }
                //     } else {
                //       console.log('xinzenghl')
                //       this.list.push(arr);
                //       length = null;
                //       break
                //     }
                //   }
                // }
              }
            }
            console.log(this.list)
          } catch (ev) {
            console.log('报错')
            return false
          }
        }
      }

    },
    //采购订单关联下拉框
    aa(e){
      let that = this
      if (e !== '' && e !== undefined){
        return request({
          url: '/purchase/ryQuality/selectBypurchaseOrderId',
          method: 'get',
          params: {linkHeadId:that.form.linkHeadId},
        }).then(function(res) {
          if (res.code === 200){
            that.tableData = res.data
          }
        })
      }else{
        that.tableData = []
      }

    },
    //删除行
    deleteRow(data) {
      this.tableData.splice(data.$index, 1)
    },
    //增加行
    addRow() {
      let newRow = { name: '', psd: '', age: '' }
      this.tableData.splice(this.tableData.length, 0, newRow)
    },
    exportExcel() {
      try {
        const $e = this.$refs['report-table'].$el
        let $table = $e.querySelector('.el-table__fixed')
        if(!$table) {
          $table = $e
        }

        const wb = XLSX.utils.table_to_book($table, {raw:true})
        const wbout = XLSX.write(wb, {bookType: 'xlsx', bookSST:true, type: 'array'})
        FileSaver.saveAs(
          new Blob([wbout],{type: 'application/octet-stream'}),
          `质检详情.xlsx`,
        )
      } catch (e) {
        if (typeof console !== 'undefined') console.error(e)
      }
    }
  }
}
</script>
